package com.MusicWeb.service;

import com.MusicWeb.dao.DB;
import com.MusicWeb.entity.Column;
import com.MusicWeb.entity.Music;
import com.MusicWeb.entity.User;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 添加音乐
 */
public class UsermusicService {
    public Music instermusic(Music music) {
        try {
            // 根据传入的user对象，执行对应的新增sql
            int a = DB.executeUpdate("INSERT INTO `music`(`music_name`, `music_singer`, `music_text`, `music_url`,  `u_id`, `user_name`) " +
                            "VALUES ( ? , ? , ? , ? , ? , ?) "
                    , music.getMusic_name()
                    , music.getMusic_singer()
                    , music.getMusic_text()
                    , music.getMusic_url()
                    , music.getUid()
                    , music.getUser_name());
            // SQL执行结果为-1，表示有数据库执行错误，返回null
            if (a == -1) {
                return null;
            }
        } catch (Exception e) {
            // 报出异常，插入失败，返回null
            e.printStackTrace();
            return null;
        }
        // 正常情况下返回user对象，表示插入成功
        return music;
    }

    /**
     * 修改音乐信息
     */
    public Music updateMusic(Music music) {
        try {
            // 根据传入的user对象，执行对应的新增sql
            int a = DB.executeUpdate("update music set music_name= ?,music_singer= ?,music_text= ? where id= ?"
                    , music.getMusic_name()
                    , music.getMusic_singer()
                    , music.getMusic_text()
                    , music.getId());
            // SQL执行结果为-1，表示有数据库执行错误，返回null
            if (a == -1) {
                return null;
            }
        } catch (Exception e) {
            // 报出异常，插入失败，返回null
            e.printStackTrace();
            return null;
        }
        // 正常情况下返回user对象，表示插入成功

        return music;
    }

    /**
     * 查询音乐
     */
    public List<Music> SelectMusic() {
        // 调用DB执行sql语句
        List<Map<String, Object>> result = DB.executeQuery("select * from music");
        List<Music> list = new ArrayList<>();
        if (result != null) {
            for (Map<String, Object> stringObjectMap : result) {
                Music music = new Music();
                music.setId((Integer) stringObjectMap.get("id"));
                music.setMusic_name((String) stringObjectMap.get("music_name"));
                music.setMusic_singer((String) stringObjectMap.get("music_singer"));
                music.setMusic_text((String) stringObjectMap.get("music_text"));
                music.setMusic_url((String) stringObjectMap.get("music_url"));
                music.setUid((Integer)stringObjectMap.get("u_id"));
                music.setUser_name((String)stringObjectMap.get("user_name") );
                list.add(music);
            }
        }
        if (list.size() > 0) {
            return list;
        }
        return null;
    }

    /*
    模糊查询
     */
    public List<Music> Musiclist(String name) {
        List<Map<String, Object>> result = DB.executeQuery("select * from music where music_name  like "
                , "%" + name + "%");

        List<Music> list = new ArrayList<>();
        if (result != null) {
            for (Map<String, Object> stringObjectMap : result) {
                Music music = new Music();
                music.setMusic_name((String) stringObjectMap.get("music_name"));
                music.setMusic_singer((String) stringObjectMap.get("music_singer"));
                music.setMusic_text((String) stringObjectMap.get("music_text"));
                music.setMusic_url((String) stringObjectMap.get("music_url"));
                music.setUid((Integer)stringObjectMap.get("u_id"));
                music.setUser_name((String)stringObjectMap.get("user_name") );
                list.add(music);
            }
        }
        if (list.size() > 0) {
            return list;
        }
        return null;
    }
    //todo: every day is a happy day
    /*
    删除音乐
     */
    public boolean del(Integer id){
        try {
            int a=DB.executeUpdate("delete from music where id = ?",id);
            if (a==-1){
                return false;
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }
    /*
    根据用户的id获取 上传的歌曲
    //todo:摸鱼的一天
     */
    public List<Music> UserMuiscColumn(Integer id){

        List<Map<String, Object>> result = DB.executeQuery("select * from music where u_id = ?",id);
        List<Music> list = new ArrayList<>();
        if (result != null) {
            for (Map<String, Object> stringObjectMap : result) {
                Music music = new Music();
//                music.setId((Integer) stringObjectMap.get("id"));
                music.setMusic_name((String) stringObjectMap.get("music_name"));
                music.setMusic_singer((String) stringObjectMap.get("music_singer"));
//                music.setMusic_text((String) stringObjectMap.get("music_text"));
//                music.setMusic_url((String) stringObjectMap.get("music_url"));
                music.setUid((Integer)stringObjectMap.get("u_id"));
                list.add(music);
            }
        }
        if (list.size() > 0) {
            return list;
        }
        return null;


    }


    /*
    用户收藏的音乐
     */
    public List<Music> CollectionMusic(Integer id){

        List<Map<String, Object>> result =
                DB.executeQuery("select * from usermusic u join music m on u.music_id=m.id  where u.user_id= ?",id);
        List<Music> list = new ArrayList<>();
        if (result != null) {
            for (Map<String, Object> stringObjectMap : result) {
                Music music = new Music();
                music.setId((Integer) stringObjectMap.get("id"));
                music.setMusic_name((String) stringObjectMap.get("music_name"));
                music.setMusic_singer((String) stringObjectMap.get("music_singer"));
                music.setMusic_text((String) stringObjectMap.get("music_text"));
                music.setMusic_url((String) stringObjectMap.get("music_url"));
                music.setUid((Integer)stringObjectMap.get("u_id"));
                list.add(music);
            }
        }
        if (list.size() > 0) {
            return list;
        }
        return null;
    }
}


